

part of appcommon;

class GestureDetectorCustomDropDownButton extends StatelessWidget {
  const GestureDetectorCustomDropDownButton(this.child, {this.popChildBuilder,
    this.ancestorContext, this.onPop,
    this.isBottom = true,
    this.iconColor,
    this.decoration,
    this.textColor,
    this.margin = 0,
    this.longPressChildBuilder,
    this.onTap,
    this.onLongTap,
    this.miniWidth,
    this.sameWidth= false,
    this.isFullWidth= false
  });

  final double margin;
  final Widget child;
  final double? miniWidth;
  final Color? iconColor;
  final Widget Function()? popChildBuilder;
  final void Function()? onTap;
  final Widget Function()? longPressChildBuilder;
  final void Function()? onLongTap;
  final Decoration? decoration;
  final bool isBottom;
  final Color? textColor;
  final bool sameWidth;
  final BuildContext? ancestorContext;
  final void Function(dynamic)? onPop;
  final bool isFullWidth;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        // decoration: decoration ?? BoxDecoration(
        //     color: Color.fromARGB(255, 234, 237, 240)
        // ),
        padding: const EdgeInsets.symmetric(vertical: 1) + const EdgeInsets.only(left: 1),
        child: child
    ).gestureDetector(onTap: (){
      onTap?.call();
      if (popChildBuilder != null) {
        _onPop(context, popChildBuilder!);
      }
    }, onLongPress: (){
      onLongTap?.call();
      if (longPressChildBuilder != null) {
        _onPop(context, longPressChildBuilder!);
      }
    });
  }

  void _onPop(BuildContext context, Widget Function() builder){
    var box = context.findRenderObject() as RenderBox;
    var rect = box.localToGlobal(Offset.zero, ancestor: ancestorContext?.findRenderObject()) & box.size;
    Navigator.of(context).push(_CustomRoute(builder: (_, __, ___) {
      if (isFullWidth) {
        return _FullWidth(builder, margin, rect);
      }
      return _PopView(rect, builder, isBottom, margin,
          miniWidth: miniWidth, needWidth: sameWidth);
    })).then((value) => onPop?.call(value));
  }
}

class CustomDropDownButton extends StatelessWidget {
  const CustomDropDownButton(this.text, {required this.popChildBuilder,
    this.ancestorContext, this.onPop,
    this.isBottom = true,
    this.iconColor,
    this.decoration,
    this.textColor,
    this.margin = 0
  });

  final double margin;
  final String text;
  final Color? iconColor;
  final Widget Function() popChildBuilder;
  final Decoration? decoration;
  final bool isBottom;
  final Color? textColor;
  final BuildContext? ancestorContext;
  final void Function(dynamic)? onPop;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        decoration: decoration ?? const BoxDecoration(
            color: Color.fromARGB(255, 234, 237, 240)
        ),
        padding: const EdgeInsets.symmetric(vertical: 1) + const EdgeInsets.only(left: 1),
        child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                  // color: Colors.white,
                  alignment: Alignment.center,
                  constraints: const BoxConstraints(minWidth: 60, minHeight: 30),
                  child: CustomText(text,
                    fontSize: 11,
                    color: textColor ?? const Color.fromARGB(255, 25, 28, 36).withOpacity(0.5),
                    fontWeight: w_bold,
                  )//].column(mainAxisAlignment: MainAxisAlignment.center),
              ),
              const SizedBox(width: 8),
              Icon(Icons.arrow_drop_down, color: iconColor),
              const SizedBox(width: 4)
            ]
        )
    ).cupertinoButton(onTap: (){
      var box = context.findRenderObject() as RenderBox;
      var rect = box.localToGlobal(Offset.zero, ancestor: ancestorContext?.findRenderObject()) & box.size;
      Navigator.of(context).push(_CustomRoute(builder: (_, __, ___) {
        return _PopView(rect, popChildBuilder, isBottom, margin);
      })).then((value) => onPop?.call(value));
    });
  }
}

class _CustomRoute extends PopupRoute {
  _CustomRoute({required this.builder});
  final Widget Function(BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation) builder;

  @override
  // TODO: implement barrierColor
  Color? get barrierColor => null;

  @override
  // TODO: implement barrierDismissible
  bool get barrierDismissible => true;

  @override
  // TODO: implement barrierLabel
  String? get barrierLabel => "";

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    // TODO: implement buildPage
    return builder(context, animation, secondaryAnimation);
  }

  @override
  // TODO: implement transitionDuration
  Duration get transitionDuration => Duration.zero;
}


class _PopView extends StatelessWidget {
  const _PopView(this.rect, this.child, this.isBottom, this.margin, {this.miniWidth
  , this.needWidth= false});
  final Rect rect;
  final Widget Function() child;
  final bool isBottom;
  final bool needWidth;
  final double margin;
  final double? miniWidth;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Stack(
        children: [
          if (isBottom)
            child().container(constraints: BoxConstraints(
                minWidth: miniWidth ?? rect.width,
                maxWidth: needWidth ? rect.width : double.infinity
            )).position(
              left: rect.left,
              top: rect.bottom + 8 + margin,
              // width: rect.width
            )
          else
            child().container(constraints: BoxConstraints(
              minWidth: miniWidth ?? rect.width,
              maxWidth: needWidth ? rect.width : double.infinity
            )).position(
              left: rect.left,
              bottom: MediaQuery.of(context).size.height - rect.top + 12 + margin,
              // width:  rect.width
            )
        ]
    );
  }
}

class _FullWidth extends StatelessWidget {
  _FullWidth(this.child, this.margin, this.rect);
  final Widget Function() child;
  final double margin;
  final Rect rect;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(left: 0,
          right: 0,
          top: rect.bottom + margin,
          child: child(),
        )
      ]
    );
  }
}